/* Produce the household roster dataset to do summary stats */

local folder "C:\Users\mlevere\OneDrive - Mathematica\Documents\Projects\Nepal\"

use "`folder'/Data/FollowUp_Raw/SEC03", clear
gen elig_type = q03_302 if q03_304a <= 2

by www hh sn, sort: egen elig_child = max(elig_type)

by www hh sn: egen newborn = max(q03_302) if elig_child == . & (q03_302 == 3 | q03_302 == 4) & q03_304a <= 17
by www hh sn: egen newborn_child = max(newborn)

replace elig_child = newborn_child if elig_child == .

drop elig_type newborn newborn_child

gen kid_type = elig_child == q03_302

gsort www hh kid_type -q03_304a -q03_304b

by www hh kid_type: gen birth_order = _n if kid_type == 1

by www hh: egen minage = min(q03_304a) if birth_order ~= .

replace birth_order = . if minage >= 13

drop elig_child kid_type minage

sort www hh sn q03_idc

rename q03_idc id_code
rename q03_301 name
rename q03_302 relationship
rename q03_303 sex
rename q03_304a age_years
rename q03_304b age_months
rename q03_305 literacy
rename q03_306 school
rename q03_307 highest_grade
rename q03_308 marital_status
rename q03_309a occupation_main
rename q03_309b occupation_secondary
rename q03_310 residing
rename q03_311 eligible
by www hh, sort: egen num_hh_members_raw = count(id_code)
by www hh, sort: egen num_hh_members_residing_raw = count(id_code) if residing == 1
by www hh, sort: egen num_eligible_women_raw = count(id_code) if eligible == 1 & age_year > 5
by www hh, sort: egen num_eligible_kids_raw = count(id_code) if eligible == 1 & age_year <= 5
by www hh, sort: egen num_hh_members = max(num_hh_members_raw)
by www hh, sort: egen num_hh_members_residing = max(num_hh_members_residing_raw)
by www hh, sort: egen num_eligible_women = max(num_eligible_women_raw)
by www hh, sort: egen num_eligible_kids = max(num_eligible_kids_raw)
drop *_raw
tempfile follow_up_raw_fam
save "`follow_up_raw_fam'", replace

/* Add on the VDC name so that we can get the treatment status */
merge m:1 www hh sn using "`folder'/Data/follow_up_raw_hh", keepusing(vdc_name district vdc_code religion) nogen
save "`follow_up_raw_fam'", replace


use "`folder'/Data/cosampling.dta", clear
keep if sampco == 1
keep district vdc control_vdc info_vdc cash_vdc
by district vdc, sort: keep if _n == 1
rename vdc vdc_name
gen district_n = 1 if district == "Ramechhap"
replace district_n = 2 if district == "Sindhuli"
replace district_n = 3 if district == "Sarlahi"
replace district_n = 4 if district == "Rautahat"
drop district
rename district_n district
merge 1:m district vdc_name using "`follow_up_raw_fam'", nogen
sort www hh sn

/* Generate one variable with all three treatment indicators, and label it,
   for the purpose of generating tables */
gen treatment = 1 if control_vdc == 1
replace treatment = 2 if info_vdc == 1
replace treatment = 3 if cash_vdc == 1

label define treatgroup 1 "Control" 2 "Info Only" 3 "Info + Cash"
label values treatment treatgroup

/* Generate the three comparison dummies so that we can do 2-way
   hypothesis testing */
gen cash_control = cash_vdc
replace cash_control = . if info_vdc == 1

gen info_control = info_vdc
replace info_control = . if cash_vdc == 1

gen cash_info = cash_vdc
replace cash_info = . if control_vdc == 1

save "`folder'/Data/follow_up_raw_fam", replace
